//
//  HCalendarCollectionViewLayout.swift
//  Drop
//
//  Created by hushaohua on 2018/5/6.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class HCalendarCollectionViewLayout: UICollectionViewLayout {
    var interval:HTimeIntervalType;
    var cellAttributes:[UICollectionViewLayoutAttributes];
    var itemSize:CGSize;
    var itemSpace:CGPoint;
    var contentInset:UIEdgeInsets = .zero;
    
    var firstWeekday:Int;//available when isDayStyle, same as DateComponents.weekday
    var itemsCount:Int;
    var fullWeeksItemCount:Int;
    func getFullItemCount() {
        if self.interval == .month {
            self.firstDayOfCurrentMonth = 0;
        }else{
            var offsetIndex = 0;
            if (self.firstWeekday == 1){
                offsetIndex = 6;
            }else{
                offsetIndex = self.firstWeekday - 2;
            }
            self.firstDayOfCurrentMonth = offsetIndex;
            var row:Int = (offsetIndex + itemsCount) / 7;
            if (offsetIndex + itemsCount) % 7 != 0{
                row = row + 1;
            }
            self.fullWeeksItemCount = row * 7;
        }
    }
    
    func getCellAttributes(){
        switch self.interval {
        case .day:
            self.getDayCellAttributes();
        case .week:
            self.getWeekCellAttributes();
        case .month:
            self.getMonthCellAttributes();
        }
    }
    
    func getSectionAttributes() {
        let lastOffsetY = self.cellAttributes.last!.frame.maxY;
        let headerAttributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, with: IndexPath(item: 0, section: 1));
        headerAttributes.frame = CGRect(x: self.contentInset.left, y: lastOffsetY + 10, width: 100, height: 30);
        self.cellAttributes.append(headerAttributes);
    }
    
    func fullItemCount() -> Int{
        switch self.interval {
        case .week:
            return self.fullWeeksItemCount;
        default:
            return self.itemsCount;
        }
    }
    
    var resultsCount:Int = 0;
    var resultsCountPerRow = 1;
    var resultSize:CGSize = .zero;
    func getResultCellAttributes(){
        self.cellAttributes.removeLast(self.cellAttributes.count - self.fullItemCount() - 1);
        if self.resultsCount == 0 {
            return;
        }
        let lastOffsetY = self.cellAttributes.last!.frame.maxY + 5;
        let offsetIndex = 0;
        var row:Int = 0;
        for index in 0..<resultsCount {
            let attribute:UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: index + offsetIndex, section: 1));
            let column = (offsetIndex + index) % resultsCountPerRow;
            row = (offsetIndex + index) / resultsCountPerRow;
            let offsetX = self.contentInset.left + (self.resultSize.width + 10) * CGFloat(column);
            let offsetY = lastOffsetY + (self.resultSize.height + 10) * CGFloat(row);
            attribute.frame = CGRect(x: offsetX, y: offsetY, width: self.resultSize.width, height: self.resultSize.height);
            self.cellAttributes.append(attribute);
        }
    }
    
    func getMonthCellAttributes(){
        self.getCellAttributes(itemCount: self.itemsCount, columns: 3, offsetIndex: 0);
    }
    
    func getDayCellAttributes() {
        self.getCellAttributes(itemCount: self.itemsCount, columns: 7, offsetIndex: self.firstDayOfCurrentMonth);
    }
    
    func getWeekCellAttributes() {
        self.getCellAttributes(itemCount: self.fullWeeksItemCount, columns: 7, offsetIndex: 0);
    }
    
    func getCellAttributes(itemCount:Int, columns:Int, offsetIndex:Int){
        self.cellAttributes.removeAll();
        var row:Int = 0;
        for index in 0..<itemCount {
            let attribute:UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: index, section: 0));
            let column = (offsetIndex + index) % columns;
            row = (offsetIndex + index) / columns;
            let offsetX = self.contentInset.left + (self.itemSize.width + self.itemSpace.x) * CGFloat(column);
            let offsetY = self.contentInset.top + (self.itemSize.height + self.itemSpace.y) * CGFloat(row);
            attribute.frame = CGRect(x: offsetX, y: offsetY, width: self.itemSize.width, height: self.itemSize.height);
            self.cellAttributes.append(attribute);
        }
    }
    
    init(interval:HTimeIntervalType) {
        self.interval = interval;
        self.firstWeekday = 1;
        self.itemsCount = 0;
        self.itemSpace = CGPoint(x: 1, y: 1);
        self.cellAttributes = [UICollectionViewLayoutAttributes]();
        self.itemSize = .zero;
        self.contentInset = .zero;
        self.fullWeeksItemCount = 0;
        super.init();
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var firstDayOfCurrentMonth = 0;
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var attributes:[UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]();
        for attribute in self.cellAttributes {
            if (rect.intersects(attribute.frame)){
                attributes.append(attribute);
            }
        }
        return attributes;
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        for attribute in self.cellAttributes {
            if (attribute.indexPath.item == indexPath.item){
                return attribute;
            }
        }
        return nil;
    }
    
    override var collectionViewContentSize: CGSize{
        let attributes = self.cellAttributes.last;
        if (self.collectionView == nil){
            return .zero;
        }else if attributes == nil{
            return .zero;
        }else{
            return CGSize(width:self.collectionView!.bounds.size.width, height:attributes!.frame.maxY + self.contentInset.bottom);
        }
    }
}
